package com.cn.chaomeng.service;

import com.cn.chaomeng.constant.DateConstant;
import com.cn.chaomeng.dao.*;
import com.cn.chaomeng.dto.FoodRetDTO;
import com.cn.chaomeng.dto.OrderHYDto;
import com.cn.chaomeng.enums.PayStateEnums;
import com.cn.chaomeng.model.*;
import com.cn.chaomeng.utils.DateUtil;
import com.cn.chaomeng.vo.app.FoodVO;
import com.cn.chaomeng.vo.app.OrderHyVO;
import com.cn.chaomeng.vo.app.OrderInfosVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 范中燊
 * @version V1.0
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: TODO
 * @date ${date} ${time}
 */
@Service
public class ExChangeService
{
    @Autowired
    private FoodtypeMapper foodTypeMapper;

    @Autowired
    private FoodMapper foodMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderfoMapper orderfoMapper;

    @Autowired
    private ExchangeDAO exchangeDAO;

    @Autowired
    private UserDoMapper userDoMapper;

    /**
     * 获取用户信息
     */
    public UserDo getUserDO(String openID)
    {
        return userDoMapper.selectByPrimaryKey(openID);
    }

    /**
     * 保存用户信息
     */
    public void saveUserDO(UserDo userDo)
    {
        userDoMapper.insert(userDo);
    }

    /**
     * 获取类型列表
     *
     * @return
     */
    public List<Foodtype> getFoodTypeList(Map map)
    {
        return foodTypeMapper.selectList(map);
    }

    /**
     * 获取商品列表
     *
     * @return
     */
    public List<Food> getFoodList(Map map)
    {
        return foodMapper.selectList(map);
    }

    /**
     * 获取商品销量
     */
    public List<FoodRetDTO> getFoodSales(Map map)
    {
        return foodMapper.selectFoodsSales(map);
    }

    /**
     * 保存订单
     *
     * @param orders
     */
    public void saveOrders(Orders orders)
    {
        ordersMapper.insert(orders);
    }

    /**
     * 保存订单详情
     *
     * @param orders
     */
    public void saveOrderfo(Orderfo orders)
    {
        orderfoMapper.insert(orders);
    }

    /**
     * 获取订单
     *
     * @param orderId
     * @return
     */
    public Orders getOrdersByKey(int orderId)
    {
        return ordersMapper.selectByPrimaryKey(orderId);
    }

    /**
     * 订单详情
     *
     * @param orderId
     * @return
     */
    public List<OrderInfosVO> getOrderInfos(int orderId)
    {
        return exchangeDAO.selectOrderInfos(orderId);
    }

    /**
     * 根据openId查询所有订单
     */
    public Map getOrderInfoByMap(Map map)
    {
        Map<String,Object> obj = new HashMap<String,Object>();

        List<OrderHYDto> orderHYDtos = new ArrayList<OrderHYDto>();

        List<OrderHyVO> hyVO = exchangeDAO.selectOrderHyVo(map);

        BigDecimal total = new BigDecimal(Double.toString(0));

        for (OrderHyVO vo : hyVO)
        {
            if (vo.getTotalcount() > 0)
            {

                total = total.add(new BigDecimal(Double.toString(vo.getTotalprices())) );

                OrderHYDto dto = new OrderHYDto();
                vo.setCreatetime(DateUtil.timeStamp2Date(vo.getCreatetime(), DateConstant.YYYY_MM_DD_HH_MM_SS));
                vo.setState(PayStateEnums.getSjjkCode(vo.getState()).getReason());
                dto.setHyVO(vo);
                dto.setInfosVOs(exchangeDAO.selectOrderInfos(vo.getOrderid()));
                orderHYDtos.add(dto);
            }
        }
        if (hyVO == null || hyVO.size() == 0)
        {
            return null;
        }
        obj.put("vo",orderHYDtos);
        obj.put("total",total);
        return obj;
    }

    /**
     * 修改订单状态
     * @param map
     */
    public void setOrderState(Map map){
        exchangeDAO.updateOrderState(map);
    }

    public String getOrderState(String orderid){
       return exchangeDAO.selectOrderState(orderid);
    }


    public List<FoodVO> selectFoodVOs(Map map){
       return exchangeDAO.selectFoodByOrderId(map);
    }



}

